package com.py.customers.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.py.customers.domain.BsCustomers;
import com.py.customers.service.IBsCustomersService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.py.api.PyCodeApi;

/**
 * 客户信息Controller
 * 
 * @author py
 * @date 2024-11-22
 */
@RestController
@RequestMapping("customers")
public class BsCustomersController extends BaseController
{
    @Autowired
    private IBsCustomersService bsCustomersService;
    @Resource
    private PyCodeApi pyCodeApi;
    /**
     * 查询客户信息列表
     */
    @PostMapping("/getcode")
    public Map<String, Object> getCode(@RequestParam(value = "code",required = false) Integer code){
        System.out.println("进来了"+code);
        return pyCodeApi.generateCode(code);
    }

    /**
     * 查所有不分页
     */
    @RequestMapping("/selectAll")
    public List<BsCustomers> selectAll(){
        System.out.println("进来了");
        return bsCustomersService.selectAll();
    }
    @RequiresPermissions("customers:customers:list")
    @GetMapping("/list")
    public TableDataInfo list(BsCustomers bsCustomers)
    {
        startPage();
        List<BsCustomers> list = bsCustomersService.selectBsCustomersList(bsCustomers);
        System.out.println("list:"+list);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @RequiresPermissions("customers:customers:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BsCustomers bsCustomers)
    {
        List<BsCustomers> list = bsCustomersService.selectBsCustomersList(bsCustomers);
        ExcelUtil<BsCustomers> util = new ExcelUtil<BsCustomers>(BsCustomers.class);
        util.exportExcel(response, list, "客户信息数据");
    }

    /**
     * 获取客户信息详细信息
     */
    @RequiresPermissions("customers:customers:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bsCustomersService.selectBsCustomersById(id));
    }

    /**
     * 新增客户信息
     */
    @RequiresPermissions("customers:customers:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BsCustomers bsCustomers)
    {

        System.out.println("bs:"+SecurityUtils.getUsername());
        bsCustomers.setCreatePersonId(SecurityUtils.getUsername());
        bsCustomers.setCreateTime(DateUtils.getNowDate());
        pyCodeApi.saveCode(bsCustomers.getCodeId(),bsCustomers.getNumber());
        return toAjax(bsCustomersService.insertBsCustomers(bsCustomers));
    }

    /**
     * 修改客户信息
     */
    @RequiresPermissions("customers:customers:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BsCustomers bsCustomers)
    {
        bsCustomers.setUpdatePersonId(SecurityUtils.getUsername());
        bsCustomers.setUpdateTime(DateUtils.getNowDate());
        return toAjax(bsCustomersService.updateBsCustomers(bsCustomers));
    }

    /**
     * 删除客户信息
     */
    @RequiresPermissions("customers:customers:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long[] id)
    {
        return toAjax(bsCustomersService.deleteBsCustomersByIds(id));
    }
    @GetMapping("/selectCustomers")
    public BsCustomers selectCustomers(@RequestParam("id") Long id){
        System.out.println("id:"+id);
        System.out.println(bsCustomersService.selectBsCustomersById(id));
        System.out.println("-----进来了2-----");
        return bsCustomersService.selectBsCustomersById(id);
    }
}
